package com.heima.wemedia.service.impl;

import com.heima.article.feign.ApArticleFeign;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.article.dtos.ArticleBehaviorDto;
import com.heima.model.article.dtos.ArticleVisitStreamMsg;
import com.heima.model.behavior.dtos.ArticleBehaviorByTimeDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.ThreadLocalUtils;
import com.heima.wemedia.service.WmStatisticsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author : Liu
 * @date : 2022/1/3 0003 23:15
 */
@Service
public class WmStatisticsServiceImpl implements WmStatisticsService {
    @Autowired
    private ApArticleFeign apArticleFeign;

    @Override
    public ResponseResult newsDimension(ArticleBehaviorByTimeDto map) {
        String begin = map.getBeginDate();
        String end = map.getEndDate();

        if (StringUtils.isEmpty(begin) || StringUtils.isEmpty(end)) {
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        try {
            //获取当前登录用户信息
            WmUser wmUser = (WmUser) ThreadLocalUtils.get();
            if(wmUser==null){
                throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
            }
            Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(begin);
            Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse(end);
            ArticleBehaviorDto articleBehaviorDto = new ArticleBehaviorDto();

            // 把日期往后推一天
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endDate);
            calendar.add(calendar.DATE,1);
            endDate=calendar.getTime();

            articleBehaviorDto.setBeginDate(beginDate);
            articleBehaviorDto.setEndDate(endDate);
            articleBehaviorDto.setUserId(wmUser.getId());
            ResponseResult<Map<String, Integer>> articleNumByUserId = apArticleFeign.findArticleNumByUserId(articleBehaviorDto);
            return ResponseResult.okResult(articleNumByUserId.getData());
        } catch (ParseException e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }finally {
            ThreadLocalUtils.remove();
        }
    }

    @Override
    public PageResponseResult newsPage(ArticleBehaviorDto dto) {
        try {
            //获取当前登录用户信息
            WmUser wmUser = (WmUser) ThreadLocalUtils.get();
            if (wmUser == null) {
                throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
            }
            dto.setUserId(wmUser.getId());
            Date endDate = dto.getEndDate();
            // 把日期往后推一天
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endDate);
            calendar.add(calendar.DATE,1);
            dto.setEndDate(calendar.getTime());

        }finally {
            ThreadLocalUtils.remove();
        }
        return apArticleFeign.findByUserIdAndTime(dto);
    }
}
